// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ItemServiceImp.java

package com.iphaeton.lms.business.course.scorm12;

import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.lms.pojo.course.*;
import com.iphaeton.persistence.IPersistenceService;
import com.iphaeton.persistence.spring.JdbcPService;
import java.io.File;
import java.util.*;
import org.apache.log4j.Logger;
import org.dom4j.*;

// Referenced classes of package com.iphaeton.lms.business.course.scorm12:
//            IItemService

public class ItemServiceImp extends PersistentServiceSupport
    implements IItemService
{

    public ItemServiceImp()
    {
    }

    public Item save(Item item)
        throws BusinessException
    {
        try
        {
            item = (Item)hbpservice.store(item);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u4FDD\u5B58\u8BFE\u7A0B\u7AE0\u8282\u5230\u6570\u636E\u5E93\u65F6\u51FA\u9519!");
        }
        return item;
    }

    public Item findItemById(String id)
        throws BusinessException
    {
        Item item = null;
        try
        {
            item = (Item)hbpservice.get(id, com.iphaeton.lms.pojo.course.Item.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6839\u636EID\u67E5\u8BE2\u8BFE\u7A0Bitem\u65F6\u51FA\u9519: " + e.getMessage() + "!");
        }
        return item;
    }

    public Document getCourseTree(String webRoot, String id, String courseId, String sn)
        throws BusinessException
    {
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("tree");
        List list = getCourseItem(courseId, sn);
        for(Iterator it = list.iterator(); it.hasNext();)
        {
            Item item = (Item)it.next();
            boolean flag = isChild(item);
            Element element = root.addElement("tree");
            element.addAttribute("text", item.getTitle());
            if(!flag)
                element.addAttribute("src", webRoot + "/coursePlayTxAction.do?method=getCourseTree&id=" + id + "&sn=" + item.getSn());
            logger.info(webRoot + "/couresPlayTxAction.do?method=getCourseTree&id=" + id + "&sn=" + item.getSn());
            if(item.getLaunch() != null && !"".equals(item.getLaunch()))
                element.addAttribute("action", "javascript:changeSco('" + id + "','" + item.getSequence() + "');");
        }

        return doc;
    }

    public Document getCourseTree(String webRoot, String id, String sn)
        throws BusinessException
    {
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("tree");
        List list = getCourseItem(id, sn);
        Element element;
        for(Iterator it = list.iterator(); it.hasNext(); element.addAttribute("openIcon", webRoot + "/images/xtree/organize2.gif"))
        {
            Item item = (Item)it.next();
            boolean flag = isChild(item);
            element = root.addElement("tree");
            element.addAttribute("text", item.getTitle());
            element.addAttribute("icon", webRoot + "/images/xtree/course.gif");
            if(!flag)
            {
                element.addAttribute("src", webRoot + "/courseMakeTxAction.do?method=courseTreeManage&id=" + id + "&sn=" + item.getSn());
                element.addAttribute("icon", webRoot + "/images/xtree/course1.gif");
            }
            element.addAttribute("action", "javascript:addItem('" + id + "','" + item.getId() + "');");
        }

        return doc;
    }

    public List getCourseItem(String courseId, String sn)
        throws BusinessException
    {
        List list = null;
        String hql = "from Item it where it.courseId='" + courseId + "' and it.sn like '" + sn + "__'";
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u627E\u8BFE\u7A0BItem\u5931\u8D25!");
        }
        return list;
    }

    public boolean isSco(Item item)
        throws BusinessException
    {
        boolean flag = true;
        String url = item.getLaunch();
        if(url != null && !url.equals(""))
            flag = false;
        return flag;
    }

    public boolean isChild(Item item)
        throws BusinessException
    {
        boolean flag = true;
        List list = null;
        String hql = "from Item it where it.courseId='" + item.getCourseId() + "' and it.sn like '" + item.getSn() + "__'";
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u9A8C\u8BC1item\u662F\u5426\u4E3A\u5B50\u8282\u70B9\u51FA\u9519!");
        }
        if(list != null && list.size() != 0)
            flag = false;
        return flag;
    }

    public Item findItemBySequence(String courseId, int sequence)
        throws BusinessException
    {
        String hql = "from Item i where i.courseId='" + courseId + "' and i.sequence='" + sequence + "'";
        Item item;
        try
        {
            List list = hbpservice.query(hql);
            if(list != null && list.size() > 0)
                item = (Item)hbpservice.query(hql).get(0);
            else
                item = null;
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u6839\u636E\u8BFE\u7A0BID\u4EE5\u53CAsequence\u67E5\u8BE2sco\u65F6\u51FA\u9519: " + e.getMessage() + "!");
        }
        return item;
    }

    public void registerPeriod(UserCourse uc, int sequence, float period)
        throws BusinessException
    {
        uc.setPeriod(uc.getPeriod() + period);
        String itemId = findItemBySequence(uc.getCourseId(), sequence).getId();
        uc.setItemId(itemId);
        try
        {
            hbpservice.store(uc);
            hbpservice.commit();
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BB0\u5F55\u8BFE\u7A0B\u5B66\u4E60\u65F6\u95F4\u4EE5\u53CA\u6700\u540E\u5B66\u4E60\u7684\u7AE0\u8282\u51FA\u9519!");
        }
        registerItemPeriod(uc.getId(), itemId, period);
    }

    private void registerItemPeriod(String id, String itemId, float period)
        throws BusinessException
    {
        String checkSql = "select period from t_user_item where user_course_id='" + id + "' and item_id='" + itemId + "'";
        List list = null;
        String sql = "";
        try
        {
            list = jdbcpservice.query(checkSql);
            if(list == null || list.size() < 1)
            {
                sql = "insert into t_user_item (user_course_id,item_id,period) values('" + id + "','" + itemId + "','" + period + "')";
            } else
            {
                float p = Float.parseFloat(((String) (((Map)list.get(0)).get("period"))));
                sql = "update t_user_item set period='" + (period + p) + "' where user_course_id='" + id + "' and item_id='" + itemId + "'";
            }
            jdbcpservice.execute(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8BB0\u5F55\u7AE0\u8282\u5B66\u4E60\u65F6\u95F4\u51FA\u9519!");
        }
    }

    public Item addItem(Course c, Item item, String item_id, String content, String text)
        throws BusinessException
    {
        if(!"".equals(content))
            item.setLaunch(item.getId() + File.separator + content);
        if(!"".equals(text))
            item.setParameter(item.getId() + File.separator + text);
        Set itemSet = c.getItem();
        String str = "";
        int j = 2;
        int sn = 1;
        int sq = 0;
        if(item_id != null && !"".equals(item_id))
        {
            Item temp = findItemById(item_id);
            j += temp.getSn().length();
            str = str + temp.getSn();
            sq = temp.getSequence().intValue();
        }
        for(Iterator it = itemSet.iterator(); it.hasNext();)
        {
            Item temp = (Item)it.next();
            if(temp.getSn().length() >= j && str.equals(temp.getSn().substring(0, j - 2)))
            {
                if(temp.getSn().length() == j)
                {
                    int i = Integer.parseInt(temp.getSn().substring(j - 2, j));
                    if(i >= sn)
                    {
                        sn = i + 1;
                        sq = temp.getSequence().intValue();
                    }
                }
                if(temp.getSn().length() > j && temp.getSequence().intValue() > sq)
                    sq = temp.getSequence().intValue();
            }
        }

        if(sn + 1 > 9)
            str = str + sn;
        else
            str = str + "0" + sn;
        item.setSn(str);
        item = save(item);
        item = changeSequence(c.getId(), item, sq + 1);
        return item;
    }

    private Item changeSequence(String id, Item item, int sq)
    {
        Item i = null;
        try
        {
            addSequence(id, sq);
            if(!item.getSn().equals("01"))
                item.setSequence(new Integer(sq));
            else
                item.setSequence(new Integer(0));
            save(item);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
        }
        return i;
    }

    private void addSequence(String id, int sq)
    {
        try
        {
            Item i = findItemBySequence(id, sq);
            if(i != null)
            {
                addSequence(id, sq + 1);
                i.setSequence(new Integer(sq + 1));
                save(i);
            }
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
        }
    }

    private static Logger logger;
    static Class class$0; /* synthetic field */
    static Class class$1; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.business.course.scorm12.ItemServiceImp.class.getName());
    }
}
